<template>
  <div>
    <div>
      <el-form style="text-align: left" :model="searchMsg" status-icon :inline="true" ref="searchFrom"
               label-width="100px" class="demo-form-inline">
        <el-form-item label="菜单标题" prop="title">
          <el-input v-model.number="searchMsg.title"></el-input>
        </el-form-item>
        <br>
        <el-form-item style="margin-left:  500px">
          <el-button type="primary" @click="search()">搜索</el-button>
          <el-button @click="restSearch()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="text-align: left">
      <el-button type="succes" @click="popAddMenu()">添加一个</el-button>
      <el-button type="danger" @click="removeSelect()">删除选中</el-button>
    </div>
    <el-table
        ref="multipleTable"
        :data="menuMsgs"
        tooltip-effect="dark"
        style="width: 100%;text-align: center"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="title"
          label="菜单标题"
      >
      </el-table-column>
      <el-table-column
          prop="id"
          label="菜单id"
      >
      </el-table-column>
      <el-table-column
          prop="pid"
          label="菜单父id"
      >
      </el-table-column>
      <el-table-column
          prop="href"
          label="路由地址"
      >
      </el-table-column>
      <el-table-column
          prop="icon"
          label="图标信息"
      >
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
          <br>
          {{ scope.row.icon }}
        </template>
      </el-table-column>
      <el-table-column
          prop="avaiable"
          label="可用状态"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.available===1">
            <el-tag type="success">可用</el-tag>
          </div>
          <div v-else>
            <el-tag type="danger">不可用</el-tag>
          </div>
          <br>
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
      >
        <template slot-scope="scope">
          <el-button @click="popModifyMenu(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="removeOne(scope.row.id)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[3,5,10,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <el-dialog
        title="添加"
        :visible.sync="addFlag"
        width="30%"
        :before-close="handleClose">
      <el-form :model="menuMsg" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="菜单标题" prop="loginname">
          <el-input v-model.number="menuMsg.title"></el-input>
        </el-form-item>
        <el-form-item label="菜单路由" prop="href">
          <el-input v-model="menuMsg.href" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="menuMsg.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父亲菜单" prop="pid">
          <el-select v-model="menuMsg.pid" placeholder="请选择父菜单">
            <el-option
                v-for="item in fatherMenu"
                :key="item.id"
                :label="item.title"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可用" prop="available">
          <el-radio v-model="menuMsg.sex" label="1">可用</el-radio>
          <el-radio v-model="menuMsg.sex" label="0">不可用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addMenu()">提交</el-button>
          <el-button @click="resetForm('userMsg')">重置</el-button>
          <el-button @click="addFlag = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!--    修改-->
    <el-dialog
        title="修改"
        :visible.sync="modifyFlag"
        width="30%"
        :before-close="handleClose">
      <el-form :model="menuMsg" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="菜单标题" prop="loginname">
          <el-input v-model.number="menuMsg.title"></el-input>
        </el-form-item>
        <el-form-item label="菜单路由" prop="href">
          <el-input v-model="menuMsg.href" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="menuMsg.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父亲菜单" prop="pid">
          <el-select v-model="menuMsg.pid" placeholder="请选择父菜单">
            <el-option
                v-for="item in fatherMenu"
                :key="item.id"
                :label="item.title"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可用" prop="available">
          <el-radio v-model="menuMsg.available" label="1">可用</el-radio>
          <el-radio v-model="menuMsg.available" label="0">不可用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="modifyMenu()">提交</el-button>
          <el-button @click="resetForm()">重置</el-button>
          <el-button @click="addFlag = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "usermenu",
  data() {

    return {
      //父亲菜单数据
      fatherMenu: [],
      // 菜单数据
      menuMsgs: [],
      // 选择框数据
      multipleSelection: [],
      // 分页数据
      pageIndex: 1,
      pageSize: 5,
      total: 0,
      // 添加提示框标记
      addFlag: false,
      // 修改的标记
      modifyFlag: false,
      // 要被添加的用户信息
      menuMsg: {},
      // 搜索的条件
      searchMsg: {},

    };
  },
  // 页面初始化方法
  created() {
    this.flushPage()
  },
  methods: {
    // 复选框的方法
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页的方法，当页面大小改变
    handleSizeChange(val) {
      this.pageSize = val
      this.flushPage()
    },
    // 当页面下标改变
    handleCurrentChange(val) {
      this.pageIndex = val
      this.flushPage()
    },
    // 删除这条记录
    removeOne(id) {
      let ids = []
      ids.push(id)
      this.removeBeach(ids)
    },
    // 删除选中
    removeSelect() {
      let ids = []
      this.multipleSelection.forEach(row => {
        ids.push(row.id)
      })
      this.removeBeach(ids)
    },
    // 批量删除记录
    removeBeach(ids) {
      const h = this.$createElement
      // 发送请求
      this.$http.post("http://127.0.0.1:8081/Menu/Remove", ids).then(resp => {
        if (resp.data.code === 2000) {
          // 消息通知
          this.$notify({
            title: "消息",
            message: h("i", {style: "color: teal"}, resp.data.msg),
            duration: 4000
          })
          // 刷新页面
          this.flushPage()
        }else{
          // 消息通知
          this.$notify.error({
            title: "错误！",
            message: h("i", {style: "color: red"}, resp.data.msg),
            duration: 4000
          })
        }
      })
    },
    // 弹出对话框
    popAddMenu() {
      this.addFlag = true
    },

    // 弹出修改
    popModifyMenu(rowMsg) {
     this.menuMsg=rowMsg
      this.modifyFlag = true
    },
    // 确定提交添加操作
    addMenu() {
      const h = this.$createElement
      this.$http.post("http://127.0.0.1:8081/Menu/Add", this.menuMsg).then(resp => {
        if (resp.data.code === 2000) {
          // 消息通知
          this.$notify({
            title: "消息",
            message: h("i", {style: "color: teal"}, " 插入成功！"),
            duration: 4000
          })
        } else {
          this.$notify({
            title: "消息",
            message: h("i", {style: "color: red"}, "插入失败"),
            duration: 4000
          })
        }
        this.handleClose()
        this.flushPage()
      })

    },
    // 修改用户信息
    modifyMenu() {
      this.$http.post("http://127.0.0.1:8081/Menu/Modify", this.menuMsg).then(resp => {
        if (resp.data.code === 2000) {
          this.$notify({
            title: "消息",
            message: "更新成功",
            duration: 4000
          })
        } else {
          this.$notify.error({
            title: '错误',
            message: '更新失败'
          });
        }
      })
      this.handleClose()
      this.flushPage()
    },
    // 对话框关闭之后
    handleClose() {
      this.$nextTick(() => {
        this.menuMsg={}
        this.$refs['ruleForm'].resetFields();
      })
      this.addFlag = false
      this.modifyFlag = false
    },
    resetForm() {
      this.menuMsg={}
      this.$nextTick(() => {
        this.$refs['ruleForm'].resetFields();
      })
    },
    // 重置搜索菜单
    restSearch() {
      this.$nextTick(() => {
        this.$refs['searchFrom'].resetFields();
      })
    },
    // 执行搜索方法
    search() {
      this.$http.post("http://127.0.0.1:8081/Menu/GetMenus", this.searchMsg).then(resp => {
        this.menuMsgs = resp.data.data
      })
    },
    // 刷新页面的方法
    flushPage() {
      this.$http.get("http://127.0.0.1:8081/Menu/List/" + this.pageIndex + "/" + this.pageSize).then(resp => {
        console.log(resp.data.data)
        // 放入菜单信息
        this.menuMsgs = resp.data.data.records;
        // 放入分页数据
        this.total = resp.data.data.total
        // 从菜单信息中筛选出来一级菜单放到fatherMenu中
        this.menuMsgs.forEach(item => {
          if (item.pid === 1 || item.pid === 0) {
            this.fatherMenu.push(item)
          }
        })
      })
    }
  }
}

</script>

<style scoped>

</style>